<template>
  <div>
     <!-- 面包屑导航区 -->
     <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">Music</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-button type="primary" @click="showadd">添加角色</el-button>
      <el-table border
    :data="tableData"
    stripe
    style="width: 100%"> 
    <el-table-column type="expand">
      
      <template #default="{row}">
        <el-row v-for="(x,y) in row.children" :key="x.id" :class="['bdbottom', y === 0 ? 'bdtop' : '', 'vcenter']">
          <!-- 一级权限 -->
          <el-col :span="5">
            <el-tag closable @close="delid(row,x.id)">{{ x.authName }}</el-tag>
            <i class="el-icon-caret-right"></i>
          </el-col>
          <!-- 二三权限 -->
          <el-col :span="19">
            <el-row v-for="(item,y) in x.children" :key="item.id" :class="['bdbottom', y === 0 ? '' : 'bdtop', 'vcenter']">
              <el-col :span="6">
                <el-tag type="success" closable @close="delid(row,item.id)">{{ item.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="18">
                <el-tag v-for="(x) in item.children" :key="x.id" type="warning" closable @close="delid(row,x.id)">{{ x.authName }}</el-tag>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        
      </template>
      
    </el-table-column>
    <el-table-column
      type='index'
      width="80">
    </el-table-column>
    <el-table-column
      prop="roleName"
      label="角色名称"
      width="580">
    </el-table-column>
    <el-table-column
      prop="roleDesc"
      label="角色描述"
      width="400">
    </el-table-column>
    <el-table-column
    
      label="操作"
      width="400">
      <template #default="{row}">
        
        <el-button type="primary" icon="el-icon-edit" @click="eddusers(row)">编辑</el-button>
        <el-button type="danger"  icon="el-icon-delete" @click="deluser(row.id)" >删除</el-button>
       <el-button type="warning" icon="el-icon-setting" @click="fq(row)">分配权限</el-button>
      </template>
    </el-table-column>
    
  </el-table>
  <!-- 对话框 -->
  <el-dialog
  title="提示"
  :visible.sync="adduser"
  width="50%"
  @close="closedig"
  >
  <el-form  ref="ruleForm" label-width="100px" :model="addform" class="demo-ruleForm">
  <el-form-item label="角色名称" prop="roleName">
    <el-input v-model="addform.roleName"></el-input>
  </el-form-item>
  <el-form-item label="角色描述" prop="roleDesc">
    <el-input  v-model="addform.roleDesc"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="adduser = false">取 消</el-button>
    <el-button type="primary" @click="addUserapi">确 定</el-button>
  </span>
</el-dialog>
<!-- 修改用户 -->
<el-dialog
  title="修改用户"
  :visible.sync="edduser"
  width="50%"
 
  >
  <el-form  ref="eddForm" label-width="100px" :model="eddform" class="demo-ruleForm">
  <el-form-item label="角色名称" prop="roleName">
    <el-input v-model="eddform.roleName"></el-input>
  </el-form-item>
  <el-form-item label="角色描述" prop="roleDesc">
    <el-input  v-model="eddform.roleDesc"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="edduser = false">取 消</el-button>
    <el-button type="primary" @click="bjadd">确 定</el-button>
  </span>
</el-dialog>
<!-- 分配权限 -->
<el-dialog
  title="提示"
  :visible.sync="fqvis"
  width="30%"
  >
  <el-tree :data="treelist" :props="treeProps" show-checkbox node-key='id' ref="treeRef" default-expand-all
   :default-checked-keys='def'></el-tree>
  <span slot="footer" class="dialog-footer">
    <el-button @click="fqvis = false">取 消</el-button>
    <el-button type="primary" @click="allotRights">确 定</el-button>
  </span>
</el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getJJ, deljj, addJJ, eddJJ, addupjj ,deletejj,fqq,fpqx} from '@/api/user'
import { Message } from 'element-ui';

export default {
  methods: {
  async  allotRights(id) {
      const keys = [...this.$refs.treeRef.getCheckedKeys(), ...this.$refs.treeRef.getHalfCheckedNodes()]
      console.log(keys);
      const idStr = keys.join(',')
      let res =await fpqx(this.roleId,{rids:idStr})
      
      if (res.meta.status != 200) {
        return Message.error('更新失败')
      }
      Message.success('更新成功')
      this.getjjList()
      this.fqvis =  false
    },
   async fq(row) {
      this.fqvis = true
      let res = await fqq()
      console.log(res);
      this.treelist = res.data
     this.getLeafKeys(row, this.def)
      this.roleId = row.id
    },
    async deluser(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(async () => {
        let res = await deletejj(id)
        if (res.meta.status == 200) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getjjList()
       }
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
     
    },
    async bjadd() {
     await addupjj(this.eddform.roleId,this.eddform)
     
      this.getjjList()
      this.edduser = false
      Message.success('修改成功')
      
    },
   async eddusers(row) {
      
      this.edduser = true
      let res = await eddJJ(row.id)
      console.log(res);
     this.eddform = res.data
    },
    closedig() {
      this.$refs.ruleForm.resetFields()
    },
   async addUserapi() {
      await addJJ(this.addform)
      // console.log(res);
      this.adduser = false
      this.getjjList()
      this.$refs.ruleForm.resetFields()
    },
    showadd() {
      this.adduser = true
      
    },
   async delid(row,itemid) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(async () => {
        
        let res = await deljj(row.id, itemid)
        if (res.meta.status === 200) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          row.children = res.data
        } else {
          Message.error('取消失败')
         }
         
         
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    async getjjList() {
    let res = await getJJ()
      console.log(res);
    this.tableData = res.data
    },
    getLeafKeys(node,arr) {
      if(!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.getLeafKeys(item,arr)
      });
    },
  },
  created() {
    this.getjjList()
  },
data() {
  return {
    roleId:'',
    def:[],
    treeProps:{
        label:'authName',
        children:'children'
      },
    treelist:[],
    addform: {
      roleName: '',
      roleDesc:''
    },
    eddform: {
      roleName: '',
      roleDesc:''
    },
    tableData: [],
    adduser:false, //添加用户对话框
    edduser: false,
    fqvis:false
  }
},
}
</script>

<style scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>